package com.eyepax.assignment.title.persistence.connection.db.oracle;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.eyepax.assignment.title.persistence.connection.base.BaseFactory;
import com.eyepax.assignment.title.persistence.connection.db.ConnectionFactory;
import com.eyepax.assignment.title.persistence.domain.SystemLogin;

/**
 * The factory Class which implements {@link ConnectionFactory}
 * 
 * @author Rukshan Silva | 02nd July 2014
 * @version 1.0.0
 */
public class OracleConnectionFactory implements ConnectionFactory {

	private static Logger log = Logger.getLogger(OracleConnectionFactory.class);

	private BaseFactory baseFactory;
	
	public OracleConnectionFactory() {
//		baseFactory.initConnection();
		
	}
	
	/**
	 * @param baseFactory the baseFactory to set
	 */
	public void setBaseFactory(BaseFactory baseFactory) {
		this.baseFactory = baseFactory;
	}


	/**
	 * Persist the given {@link Object} to the relational database
	 */
	public void save(Object object) {
		
		Session session = baseFactory.beginTransaction();
		Transaction transaction = session.beginTransaction();
		
		session.persist(object);
		transaction.commit();
	}
	
	public void updateLoggedInClient(String sessionId) {
		
		Session session = baseFactory.beginTransaction();

		SystemLogin result = (SystemLogin) session.createCriteria(SystemLogin.class).add(
				Restrictions.eq("sessionId", sessionId)).uniqueResult();
		
		if (result != null) {
			log.info("[SUCCESS] MarketWatch Loaded from Database");
//			return result;
		
		} else {
			log.info("[FAIL] MarketWatch does not exist in the Database");
//			return null;				
		}	
	}
}